Main site

Built-in Modules > Currency Class

Currency Class

Overview

The Currency class is a utility for currency conversion, supporting both single and multiple amount conversions.

Class Definition

class Currency {
  constructor() {
    // Class constructor
  }

  async convert(amount, from_currency_iso_code, to_currency_iso_code) {
    // Method to convert currency
  }
}

Usage Example

const currency = new Currency();
const converted_amount = await currency.convert(amount, from_currency_iso_code, to_currency_iso_code);

This example illustrates the initialization and use of the Currency class to convert an amount (or multiple amounts) from one currency to another. By providing the amount, source currency ISO code (from_currency_iso_code), and target currency ISO code (to_currency_iso_code), the method returns the equivalent amount in the target currency.

Notes

  • The amount argument can be either a single numerical value or an array of numerical values.
  • If an array of amounts is provided, the converter returns an array of corresponding converted amounts, facilitating batch conversions.

The currency rate is based on the real-time rates provided by Shopify on https://cdn.shopify.com/s/javascripts/currencies.js